package com.yang.lesson02;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestActionTwo {

    public static void main(String[] args) {
        //两个按钮，实现同样一个监听
        //开始   结束
        Frame frame = new Frame("开始-停止");
        Button button1 = new Button("start");
        Button button2 = new Button("stop");

        frame.pack();
        frame.setLayout(new BorderLayout());
        frame.setVisible(true);

        //setActionCommand()  设置按钮的信息
        //可以显示地定义触发会返回的命令，如果不显示定义，则会走默认地值
        //可以多个按钮只写一个监听类
        button1.setActionCommand("begin");

        MyMonitor myMonitor = new MyMonitor();
        button1.addActionListener(myMonitor);
        button2.addActionListener(myMonitor);

        frame.add(button1,BorderLayout.NORTH);
        frame.add(button2,BorderLayout.SOUTH);

        windowsClose(frame);
    }
    //关闭窗体的事件
    private static void windowsClose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

class MyMonitor implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        //e.getActionCommand() 获得按钮的信息
        System.out.println("按钮被点击" + e.getActionCommand());
    }
}